﻿//======================================================================
//  记录的分页
/* Demo:
 * var pageCount = 0;
   articleResult = PageHelper<ArticleModel>.RenderPage(articleResult, pageIndex, pageSize, out pageCount);
*/     
using System.Collections.Generic;
namespace Common
{
    /// <summary>
    /// 分页辅助类
    /// </summary>
    /// <typeparam name="T">Class</typeparam>
    public class PageHelper<T>
    {
        /// <summary>
        /// 分页辅助
        /// </summary>
        /// <param name="source">要分页的数据集合</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页的记录条数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>当前请求的记录</returns>
        public static List<T> RenderPage(List<T> source, int pageIndex, int pageSize, out int pageCount)
        {
            pageCount = 0;
            if(source == null) return null;
            if(source.Count.Equals(0)) return null;
            if(source.Count <= pageSize) return source;
            pageCount = (source.Count + pageSize - 1) / pageSize;
            //Check the index
            if (pageIndex < 0) pageIndex = 0;
            if (pageIndex >= pageCount) pageIndex = pageCount - 1;
            //Get startIndex
            var startIndex = 0;
            if (pageIndex > 0) startIndex = pageSize * pageIndex;
            var length = pageSize;
            //Get the subIndex
            var subIndex = source.Count / pageSize;
            if (subIndex.Equals(0)) //if the last size equal source size
                length = source.Count;
            else
            {
                //get last size
                if (pageIndex.Equals(source.Count / pageSize))
                    length = source.Count % pageSize;
            }
            return source.GetRange(startIndex, length);
        }

        /// <summary>
        /// 对大段文字进行分页
        /// </summary>
        /// <param name="value">文字信息</param>
        /// <param name="pageLength">每页文字的大小</param>
        /// <param name="isClearHtml">是否要清除HTML标签</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>当前请求的文字信息</returns>
        public static string PageString(string value, int pageLength, bool isClearHtml, int pageIndex, out int pageCount)
        {
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            if (isClearHtml) value = HTML.ClearHTMLTag(value);
            var length = value.Length;
            pageCount = (length + pageLength - 1) / pageLength;
            if (pageIndex > pageCount) pageIndex = pageCount - 1;
            var startIndex = pageIndex * pageLength;
            return pageCount.Equals(pageIndex + 1) ? value.Substring(startIndex) : value.Substring(startIndex, pageLength);
        }

        /// <summary>
        /// Cut String
        /// </summary>
        /// <param name="value">Content with the cut</param>
        /// <param name="len">Keep the length</param>
        /// <param name="ect">Ect ...</param>
        /// <returns>The new cut content</returns>
        public static string CutString(string value, int len, string ect)
        {
            if (string.IsNullOrEmpty(value) || len <= 0)
            {
                return string.Empty;
            }

            var l = value.Length;

            #region
            var clen = 0;
            while (clen < len && clen < l)
            {
                //每遇到一个中文，则将目标长度减一。
                if (value[clen] > 128) { len--; }
                clen++;
            }
            #endregion

            if (clen < l)
            {
                return value.Substring(0, clen) + ect;
            }
            return value;
        }

    }
}
